<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
  <!-- 导入样式表 -->
  <link rel="stylesheet" href="./assets/news.css" />
</head>

<body>

  <!-- 新闻列表 -->
  <div id="news-list">
    <!-- 新闻的 item 项 -->
    <!-- <div class="news-item">
      <img class="thumb" src="" alt="" />
      <div class="right-box"> -->
        <!-- 新闻标题 -->
        <!-- <h1 class="title">5G商用在即，三大运营商营收持续下降</h1>
        <div class="footer">
          <div> -->
            <!-- 新闻来源 -->
            <!-- <span>胡润百富</span>&nbsp;&nbsp;
            发布日期
            <span>2019-10-28 10:14:38</span>
          </div> -->
          <!-- 评论数量 -->
          <!-- <span>评论数：66</span>
        </div>
      </div>
    </div> -->
  </div>

  <!-- 
    目标:
      1. 获取数据
         通过 ajax 发送请求 get
         请求地址: http://localhost:3000/api/news
      2. 渲染页面
  -->
  <script src="./lib/axios(1).js"></script>
  <!-- <script src="./lib/ajax.js"></script> -->
  <!-- <script src="./lib/jquery.js"></script> -->
  <script>
    const list=document.querySelector('#news-list')
    // 1.获取数据
   function initNew(){
    axios({
      method:'GET',
      url:'http://localhost:8080/api/news'
    }).then(({data:res})=>{
if(res.code===200){
  list.innerHTML=res.data.map((item)=>{
    return `
    <!-- 新闻的 item 项 -->
    <div class="news-item">
     <img class="thumb" src="../server/data${item.img}" alt="" />
     <!-- <img class="thumb" src="../server/data/images/${item.id-1}.webp" alt="" /> -->
      <div class="right-box">
        <!-- 新闻标题 -->
        <h1 class="title">${item.title}</h1>
        <div class="footer">
          <div>
            <!-- 新闻来源 -->
            <span>${item.source}</span>&nbsp;&nbsp;
            <!-- 发布日期 -->
            <span>${item.time}</span>
          </div>
          <!-- 评论数量 -->
          <span>评论数：${item.cmtcount}</span>
        </div>
      </div>
    </div>
    
    `
  }).join('')
}
    })
   }
   initNew()
  </script>
</body>

</html>